﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Configuration;
using System.Xml;

namespace CECB.Dashbord
{
    /// <summary>
    /// Interaction logic for DatabaseSettingWindow.xaml
    /// </summary>
    public partial class DatabaseSettingWindow : Window
    {
        public DatabaseSettingWindow()
        {
            InitializeComponent();
        }

        private void buttonExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            if (isValidate())
            {
                
                String pcName = textBoxPcName.Text.Trim();
                String serverName = textBoxServerName.Text.Trim();
                String dbName = textBoxDbName.Text.Trim();
                String secrity = comboBoxSecity.SelectedIndex == 0 ? true.ToString() : false.ToString();
                String password = textBoxPassword.Password.ToString();
                UpdateConnectionString1(pcName, dbName, secrity, "DSSEntities");
                UpdateConnectionString2(pcName, dbName, secrity, "CECB.Properties.Settings.DSSConnectionString", "");
            }
        }


        private Boolean isValidate()
        {
            bool isValide = false;

            if (textBoxPcName.Text.Trim().ToString().Length > 0)
            {

                if (textBoxServerName.Text.Trim().Length > 0)
                {
                    if (textBoxDbName.Text.Trim().Length > 0)
                    {
                        if (comboBoxSecity.SelectedIndex > -1)
                        {
                            if (textBoxPassword.Password.Length > 0)
                            {
                                isValide = true;
                            }
                            else
                                MessageBox.Show("Enter server Password");

                        }
                        else
                            MessageBox.Show("Select security type");
                    }
                    else
                        MessageBox.Show("Enter server name");
                }

                else
                    MessageBox.Show("Enter server name");
            }
            else
            {

                MessageBox.Show("Enter PC name");
            }

            return isValide;
        }


        private void UpdateConnectionString1(String pcName, String dbName, String security, string configName)
        {
            XmlDocument appDoc = new XmlDocument();
            string appConfigFile = System.IO.Directory.GetCurrentDirectory() + @"\CECB.exe.config";
            appDoc.Load(appConfigFile);

            String consStrinPart = @"metadata=res://*/DSSModel.csdl|res://*/DSSModel.ssdl|res://*/DSSModel.msl;provider=System.Data.SqlClient;" +
                                    @"provider connection string=&quot;" +
                                    @"Data Source=" + pcName + ";" +
                                    @"Initial Catalog=" + dbName + ";" +
                                    @"Integrated Security=" + security + ";" +
                                    @"MultipleActiveResultSets=True;" +
                                    @"Application Name=EntityFramework&quot;";

            string connectionStringXPath = "/configuration/connectionStrings/add[@name=\"{0}\"]";
            connectionStringXPath = string.Format(connectionStringXPath, configName);
            XmlNode node = appDoc.SelectSingleNode(connectionStringXPath);
            node.Attributes["connectionString"].Value = consStrinPart;
            appDoc.Save(appConfigFile);

        }

        private void UpdateConnectionString2(String pcName, String dbName, String security, string configName, String password)
        {
            XmlDocument appDoc = new XmlDocument();
            string appConfigFile = System.IO.Directory.GetCurrentDirectory() + @"\CECB.exe.config";
            appDoc.Load(appConfigFile);
            String consStrinPart = String.Empty;
            consStrinPart = @"Data Source=" + pcName + ";" + "Initial Catalog=" + dbName + ";Persist Security Info=" + security + "";
            if (password.Length > 0)
            {
                consStrinPart = @"Data Source=" + pcName + ";" + "Initial Catalog=" + security + ";Persist Security Info=" + security + ";User ID=sa;Password=" + password + "";
            }

            string connectionStringXPath = "/configuration/connectionStrings/add[@name=\"{0}\"]";
            connectionStringXPath = string.Format(connectionStringXPath, configName);
            XmlNode node = appDoc.SelectSingleNode(connectionStringXPath);
            node.Attributes["connectionString"].Value = consStrinPart;
            appDoc.Save(appConfigFile);

        }

         
       
    }
}
